import { Room } from './Room';

export class RoomManager {
    private rooms: Map<string, Room>;

    constructor() {
        this.rooms = new Map<string, Room>();
    }

    public async initialize(): Promise<void> {
        // 初始化房间管理器
    }

    public async handleJoinRoom(req: { roomId: string }, playerId: string): Promise<{ roomId: string }> {
        let room = this.rooms.get(req.roomId);
        
        if (!room) {
            room = new Room(req.roomId);
            this.rooms.set(req.roomId, room);
        }

        await room.addPlayer(playerId);
        return { roomId: req.roomId };
    }

    public async handlePlayerMove(msg: { direction: string }, playerId: string): Promise<void> {
        // 查找玩家所在的房间
        for (const room of this.rooms.values()) {
            if (room.hasPlayer(playerId)) {
                await room.handlePlayerMove(playerId, msg.direction);
                break;
            }
        }
    }

    public getRoom(roomId: string): Room | undefined {
        return this.rooms.get(roomId);
    }
} 